﻿using Abp.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.AutoMapper;

namespace MyCompanyName.AbpZeroTemplate.Prm.Admin
{
    public class LaboratoryAppService : AbpZeroTemplateAppServiceBase, ILaboratoryAppService
    {
        IRepository<Business, int> businessRepository;
        IRepository<RdCenter, int> rdCenterRepository;
        IRepository<Laboratory, int> laboratoryRepository;


        public LaboratoryAppService(IRepository<Laboratory, int> _laboratoryRepository, IRepository<Business, int> _businessRepository, IRepository<RdCenter, int> _rdCenterRepository)
        {
            laboratoryRepository = _laboratoryRepository;
            businessRepository = _businessRepository;
            rdCenterRepository = _rdCenterRepository;

        }

        public List<Dto.GetLaboratoryOutPut> GetAll()
        {
            var all = laboratoryRepository.GetAll();
            var model = all.MapTo<List<Dto.GetLaboratoryOutPut>>();
            return model;
        }


        public List<Dto.GetLaboratoryOutPut> GetByCenterId(int rdCenterId)
        {
            var all = laboratoryRepository.GetAll().Where(x => x.RdCenterId == rdCenterId);
            var model = all.MapTo<List<Dto.GetLaboratoryOutPut>>();
            return model;
        }


        public async Task CreateOrEdit(Dto.CreateOrEditLaboratoryInPut input)
        {
            var lab = new Laboratory();
            if (input.Id > 0)
            {
                lab = await laboratoryRepository.GetAsync(input.Id);
            }
            else
            {
                await laboratoryRepository.InsertAsync(lab);
            }
            lab.Name = input.Name;
            lab.RdCenterId = input.RdCenterId;
            lab.Principal = input.Principal;
            lab.PrincipalId = input.PrincipalId;
        }


        public async Task<Dto.GetLaboratoryOutPut> Get(int laboratoryId)
        {
            var lab = await laboratoryRepository.GetAsync(laboratoryId);
            var model = lab.MapTo<Dto.GetLaboratoryOutPut>();
            return model;
        }


        /// <summary>
        /// 事业部、研发中心、研究室树
        /// </summary>
        /// <returns></returns>
        public async Task<List<Dto.GetLaboratoryTree>> GetTree()
        {
            List<Dto.GetLaboratoryTree> result = new List<Dto.GetLaboratoryTree>();
            var business = await businessRepository.GetAllListAsync();
            var rdCenter = await rdCenterRepository.GetAllListAsync();
            var laboratory = await laboratoryRepository.GetAllListAsync();
            foreach (var bus in business)
            {
                var busId = Guid.NewGuid().ToString();
                var busNode = new Dto.GetLaboratoryTree()
                {
                    id = busId,
                    key = bus.Id,
                    keyType = "business",
                    leaf = false,
                    parentId = "0",
                    text = bus.Name,
                    expanded = true,
                    children = new List<Dto.GetLaboratoryTree>()
                };
                foreach (var rd in rdCenter.Where(x => x.BusinessId == bus.Id))
                {
                    var rdId = Guid.NewGuid().ToString();
                    var centerNode = new Dto.GetLaboratoryTree()
                    {
                        id = rdId,
                        key = rd.Id,
                        firstKeyId = bus.Id,
                        keyType = "center",
                        leaf = false,
                        parentId = busId,
                        text = rd.Name,
                        expanded = true,
                        children = new List<Dto.GetLaboratoryTree>()
                    };
                    busNode.children.Add(centerNode);
                    foreach (var lab in laboratory.Where(x => x.RdCenterId == rd.Id))
                    {
                        var labId = Guid.NewGuid().ToString();
                        var labNode = new Dto.GetLaboratoryTree()
                        {
                            id = labId,
                            key = lab.Id,
                            firstKeyId = bus.Id,
                            secondKeyId = rd.Id,
                            keyType = "laboratory",
                            leaf = true,
                            parentId = rdId,
                            text = lab.Name,
                            expanded = true,
                            children = new List<Dto.GetLaboratoryTree>()
                        };
                        centerNode.children.Add(labNode);
                    }
                }
                result.Add(busNode);
            }
            return result;
        }


        public async Task Delete(int laboratoryId)
        {
            await laboratoryRepository.DeleteAsync(laboratoryId);
        }
    }
}
